// JavaScript Document
//此模块需要匹配相应的css
define(function (require, exports, module) {

	var config={"console":{}}
	config.console.currentShowType="all";											//记录当前显示的模式，
	config.console.SHOWTYPE_BY_CONSOLE="showTypeByConsole";							//记录使用console对象输出
	config.console.SHOWTYPE_BY_CONTROL="showTypeByControl";							//记录使用控制台输出
	config.console.SHOWTYPE_BY_SEND_AJAX="showTypeBySendAjax";						//记录使用ajax输出
	config.console.SHOWTYPE_NONE="none";
	config.console.user="master";
	config.console.master={
		"regulation":{
			"log":[config.console.SHOWTYPE_BY_CONSOLE,config.console.SHOWTYPE_BY_CONTROL,config.console.SHOWTYPE_BY_SEND_AJAX],
			"info":[config.console.SHOWTYPE_BY_CONSOLE,config.console.SHOWTYPE_BY_CONTROL,config.console.SHOWTYPE_BY_SEND_AJAX],
			"debug":[config.console.SHOWTYPE_BY_CONSOLE,config.console.SHOWTYPE_BY_CONTROL,config.console.SHOWTYPE_BY_SEND_AJAX],
			"warn":[config.console.SHOWTYPE_BY_CONSOLE,config.console.SHOWTYPE_BY_CONTROL,config.console.SHOWTYPE_BY_SEND_AJAX],
			"error":[config.console.SHOWTYPE_BY_CONSOLE,config.console.SHOWTYPE_BY_CONTROL,config.console.SHOWTYPE_BY_SEND_AJAX]
		}
	}
	config.console.looker={
		"regulation":{
			"log":[config.console.SHOWTYPE_NONE],
			"info":[config.console.SHOWTYPE_NONE],
			"debug":[config.console.SHOWTYPE_NONE],
			"warn":[config.console.SHOWTYPE_NONE],
			"error":[config.console.SHOWTYPE_NONE]
		}
	}
	config.console.showType=["all","log","error","warn","info","debug"];	
	
	
	
	var Console=function(){
		var self=this;
		self.element=$("#consoleTableWrapper",window.top.document).get(0);
		//此变量记录的是所有的信息
		self.recordMessage=[];
		if(arguments.length==1&&$.type(arguments[0])=="object"){
			config.console=$.extend({},config.console,arguments[0]);
		}
		this.init();
	};
	Console.config=config.console;
	//添加html元素的方法
	Console.prototype.addDivAction=function(){
		var self=this;
		var addDiv=$("<div>");
		addDiv.attr("id","consoleTableWrapper");
		addDiv.html('<div class="consoleTableTitle">'+
				'<div title="关闭" class="close">x</div>'+
				'<div title="最大化" class="max">□</div>'+
				'<div title="最小化" class="min">-</div>'+
				'<div title="全部" class="all">全部</div>'+
				'<div title="日志" class="log">日志</div>'+
				'<div title="错误" class="error">错误</div>'+
				'<div title="警示" class="warn">警示</div>'+
				'<div title="bug" class="debug" >调试</div>'+
				'<div title="信息" class="info" >信息</div>'+
				'<div title="清空" class="clear" >清空</div>'+
			'</div>'+
			'<div class="consoleTable" >'+
			'<table >'+
				'<thead>'+
				'<tr>'+
					'<th class="type">类型</th>'+
					'<th>说明</th>'+
					'<th class="time">时间</th>'+
				'</tr>'+
				'</thead>'+
				'<tbody id="insetRecord">'+
					'<tr>'+
						'<td>1</td>'+
						'<td>2</td>'+
						'<td>3</td>'+
					'</tr>'+
				'</tbody>'+
			'</table>'+
			'</div>');
		$(window.top.document.body).append(addDiv);
		self.element=addDiv.get(0);
		return true;
	}
	//view填充
	Console.prototype.controlHtmlAction=function(praObject,parentObject){
		if(config.console.currentShowType!="all"&&config.console.currentShowType!=praObject.type){
			return false;
		}
		var a=$("<tr>");
		a.html('<td class='+praObject.type+'>'+praObject.type+'</td>'+'<td>'+praObject.message+'</td>'+'<td><span class="date">'+praObject.date+'</span><span class="time">'+praObject.time+'</span></td>');
		parentObject.append(a);
	}
	//布置整个html，css，事件的所有方法的入口。
	Console.prototype.showConsoleAction=function(){
		var self=this;
		$(document).bind("keydown",function(event){self.consolekeyDownListen(event,self)});
	}
	Console.prototype.bind=function(selector){
		var self=this;
		$(selector).bind("keydown",function(event){self.consolekeyDownListen(event,self)});
	}
	Console.prototype.consolekeyDownListen=function(event,self){
		var event=event||window.event;
		var self=self;
		if(event.altKey&&event.ctrlKey&&event.keyCode==67){
			if($(self.element).length==0){
				var a=self.addDivAction();
				$(window.top.document).find("body").addClass("consoleOpen");
				if(a){
					self.bindControleAction();
					self.emptyAndFilltable();
				}
			}
		}
		
	}
	//初始化。
	Console.prototype.init=function(){
		this.showConsoleAction();
	}
	//绑定事件的入口
	Console.prototype.bindControleAction=function(){
		var self=this;
		$(self.element).find(".max").bind("click",function(){self.maxDiv();});
		$(self.element).find(".min").bind("click",function(){self.minDiv();});
		$(self.element).find(".close").bind("click",function(){self.closeDiv();});
		$(self.element).find(".log").bind("click",function(){self.emptyAndFilltable("log");});
		$(self.element).find(".all").bind("click",function(){self.emptyAndFilltable();});
		$(self.element).find(".debug").bind("click",function(){self.emptyAndFilltable("debug");});
		$(self.element).find(".info").bind("click",function(){self.emptyAndFilltable("info");});
		$(self.element).find(".warn").bind("click",function(){self.emptyAndFilltable("warn");});
		$(self.element).find(".error").bind("click",function(){self.emptyAndFilltable("error");});
		$(self.element).find(".clear").bind("click",function(){self.clearBody();});
		$(self.element).find("th.time").bind("click",function(){self.dateTypeToggle()});
	}
	Console.prototype.dateTypeToggle=function(){
		var self=this;
		$(".consoleTable",self.element).hasClass("showDate")?$(".consoleTable",self.element).removeClass("showDate"):$(".consoleTable",self.element).addClass("showDate")
	}
	//清空事件
	Console.prototype.clearBody=function(){
		var self=this;
		if($("#insetRecord")){
			$("#insetRecord").empty()
		}
		if(self.recordMessage){
			self.recordMessage=[];
		}
	}
	//关闭事件
	Console.prototype.closeDiv=function(){
		var self=this;
		$(self.element).remove();
		$(window.top.document).find("body").removeClass("consoleOpen");
		self.element="";
	}
	//最小化事件
	Console.prototype.minDiv=function(){
		var self=this;
		$(self.element).addClass("minType");
		$(window.top.document).find("body").removeClass("consoleOpen");
	}
	//最大化事件
	Console.prototype.maxDiv=function(){
		var self=this;
		$(self.element).removeClass("minType");
		$(window.top.document).find("body").addClass("consoleOpen");
	}
	//清空并且填充数据
	Console.prototype.emptyAndFilltable=function(str){
		var self=this;
		console.log($(self.element));
		var fatherDiv=$(self.element).find("#insetRecord");
		fatherDiv.empty();
		var insetData=[];
		config.console.currentShowType=str?str:"all";
		if(config.console.currentShowType!="all"){
			for(var i=0;i<self.recordMessage.length;i++){
				if(self.recordMessage[i].type&&self.recordMessage[i].type==config.console.currentShowType){
					insetData.push(self.recordMessage[i]);
				}
			}
		}else{
			insetData=self.recordMessage;
		}
		for(var i=0;i<insetData.length;i++){
			var b=insetData[i];
			self.controlHtmlAction(b,fatherDiv);
		}
	}
	//填充一条数据
	Console.prototype.fillOneData=function(praObject){
		var self=this;
		self.recordMessage.push(praObject)
	}
	//填充一条表格
	Console.prototype.fillOneTable=function(praObject){
		var self=this;
		var fatherDiv=$(self.element).find("#insetRecord");
		if(fatherDiv.length<1){
			return false;	
		}
		self.controlHtmlAction(praObject,fatherDiv);
	}
	Console.prototype.log=function(object){
		this.show(object,"log");
	}
	Console.prototype.debug=function(object){
		this.show(object,"debug");
	}
	Console.prototype.info=function(object){
		this.show(object,"info");
	}
	Console.prototype.warn=function(object){
		this.show(object,"warn");
	}
	Console.prototype.error=function(object){
		this.show(object,"error");
	}
	//根据不同的权限使用不同的方法。
	Console.prototype.show=function(object,type){
		var self=this;
		var rule=(config.console[config.console.user]).regulation;
		rule=rule[type];
		if(rule.length<1){
			console.log("self.regulation[config.console.user]没有取得相应的值");				
			return false;
		}
		for(var i=0;i<rule.length;i++){
			switch(rule[i]){
				case config.console.SHOWTYPE_BY_CONSOLE:
					console.log(object);	
					break;
				case config.console.SHOWTYPE_BY_CONTROL:
					var date =new Date();
					var time =date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+"."+date.getMilliseconds(); 
					var recDate=parseInt(date.getFullYear())+":"+(parseInt(date.getMonth())+1)+":"+date.getDate();
					var praObject={"type":type,"message":self.showObjectHelper(object),"time":time,"date":recDate}
					self.fillOneData(praObject);
					self.fillOneTable(praObject);
					break;
				case config.console.SHOWTYPE_BY_SEND_AJAX:
					 console.log("使用ajax传递数据的方法尚未编写");
					 break;
				case config.console.SHOWTYPE_NONE:
					break;
				default:
					console.log("出现了没有匹配的关键字值是"+rule[i]);
					return false;
				break;
			}
		}
		//self.recordMessage.push({"type":type,"message":self.showObjectHelper(object),"time":new Date()})
		
	}
	Console.prototype.showObjectHelper=function(object){
		var self=this;
		switch($.type(object)){
		case "object":
			if(JSON){
				return JSON.stringify(object);
			}else{
				self.log("不支持JSON");
				var returnStr="{";
				for(var a in object){
					returnStr+=a+":"+object[a];
					var b=Object.keys(object)[Object.keys(object).length-1];
					if(b!=a){
						returnStr+=",";
					}
				}
				returnStr+="}";
			}
			break;
		default:
			return object.toString();
			break;
		}
	}
	module.exports = Console;
});